var param = JSON.parse(localStorage.getItem('param'));
var supplyId ;
if(param){
	supplyId = param.supplyId;
}
$(function(){
	init();
	initButton();
});
//初始化页面
function init(){
	if(supplyId){
		//回显
		$.ajax({
			type : "get",
			url : server_url
					+ 'rest/tea/product/getTeaProductInfo.do?ran=' + Math.random(),
			dataType : 'json',
			async : true,
			data : {
				token : token,
				productId:supplyId
			},
			success : function(result) {
				console.log(result);
				if (result.retCode == '0000') {
					var product = result.data;
					$('#name').val(product.productname);
					$('#teaPhone').val(product.teaPhone);
					$('#detailAddress').val(product.detailAddress);
					$('#teaArea').val(product.teaArea||'');
					$('#teaHight').val(product.teaHight||'');
					$('#coverArea').val(product.coverArea||'');
					$('#teaLocation').val(product.teaLocation||'');
					$('#teaPrice').val(product.teaPrice||'');
					$('#desception').val(product.desception||'');
					$('#teaNotice').val(product.teaNotice||'');
					
					var gandenTypes = $("input[name='gandenType']");
					gandenTypes.removeProp('checked');
					if(product.gandenType){
						$(gandenTypes[product.gandenType]).prop("checked",true);
					}
					form.render('radio');
					
					/*var teaIndustrys = $("input[name='teaIndustry']");
					teaIndustrys.removeProp('checked');
					var ss = product.teaIndustry.split(",");
					$.each(ss,function(i,n){
						$(teaIndustrys[n]).prop("checked",true);
					});*/
					
					var teaTypes = $("input[name='teaType']");
					teaTypes.removeProp('checked');
					if(product.teaType){
						var ss = product.teaType.split(",");
						$.each(ss,function(i,n){
							$(teaTypes[n]).prop("checked",true);
						});
					}
					form.render('checkbox');
					
				}else{
					showErrorMsg(result.data);
				}
			}
		});
	}
}
//初始化绑定事件
function initButton(){
	$('.address-btn-box').on('click',function(){ 
		var name = $('#name').val();
		var teaPhone = $('#teaPhone').val();
		var detailAddress = $('#detailAddress').val();
		var teaArea = $('#teaArea').val();
		var teaHight = $('#teaHight').val();
		var coverArea = $('#coverArea').val();
		var teaLocation = $('#teaLocation').val();
		var teaPrice = $('#teaPrice').val();
		var desception = $('#desception').val();
		var teaNotice = $('#teaNotice').val();
		
		var gandenType = $('input:radio:checked').val();
		
		var $teaType = $('input:checkbox:checked');
		
		var teaType = '';
		
		$.each($teaType,function(i,n){
			teaType += $(this).val()+",";
		});
		
		if(teaType.length>1){
			teaType = teaType.substring(0,teaType.length-1)
		}
		
		if(!checkNull(name,"请输入联系人姓名")){
			return;
		}
		if(!checkNull(teaPhone,"请输入联系人电话")){
			return;
		}
		if(!checkNull(detailAddress,"请输入联系人地址")){
			return;
		}
		
		$.ajax({
			type : "post",
			url : server_url
					+ 'rest/tea/supply/demand1.do?ran=' + Math.random(),
			dataType : 'json',
			async : true,
			data : {
				token : token,
				name :name,
				teaPhone :teaPhone,
				detailAddress :detailAddress,
				teaArea :teaArea,
				teaHight :teaHight,
				coverArea :coverArea,
				teaLocation :teaLocation,
				teaPrice :teaPrice,
				desception :desception,
				teaNotice :teaNotice,
				gandenType:gandenType,
				teaType:teaType,
				supplyId:supplyId,
				lat:localStorage.getItem("lat"),
				lon:localStorage.getItem("lon"),
				userId:localStorage.getItem("userid")
			},
			success : function(result) {
				console.log(result);
				if (result.retCode == '0000') {
					jumpToIndex("发布成功",2000);
				}else{
					showErrorMsg(result.data);
				}
			}
		});
	});
}